package com.csyf.manager.channel.nmpay;

import com.alibaba.fastjson.JSONObject;
import com.csyf.manager.channel.base.PaymentVo;
import com.csyf.manager.channel.base.TrandeVo;
import com.csyf.manager.entity.SysAgentChannel;
import com.csyf.manager.entity.SysPaymentChannel;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.impl.RedisService;
import com.csyf.manager.utils.HttpHelper;
import com.csyf.manager.utils.NumberHelpler;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: Enzo
 * @Date: 2019/4/3 15:24
 */
@Slf4j
@Service
public class NmPayBiz {

    public static final String HOST_URL = "http://www.51vzsn.cn/";

    @Autowired
    private RedisService redisService;

    public String dopay(TrandeVo vo, String srcMerchId) throws ApiValidateException

    {

        String channelStr = redisService.getSrcChannelInfo(vo.getMch_id(), srcMerchId);
        SysAgentChannel sysAgentChannel = new Gson().fromJson(channelStr, SysAgentChannel.class);
        Map<String, String> map = new TreeMap<>();
        map.put("merchant_id", sysAgentChannel.getMerchantNo());
        map.put("reg_merchant_id", "");

        map.put("order_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        map.put("order_no", vo.getOut_trade_no());
        map.put("subject", vo.getBody());
        map.put("order_amount", NumberHelpler.cent2money(vo.getTotal_fee()).toString());
        map.put("timeout_express", "1d");
        map.put("bank_type", "ZFB-5");
        map.put("notify_url", sysAgentChannel.getNotifyUrl());
        map.put("return_url", vo.getReturn_url());
        map.put("bank_accno", "");
        map.put("user_id", "1321346546");
        MD5SignUtils.signMap(map, sysAgentChannel.getSignKey());

        String s = HttpHelper.doPostForm(HOST_URL + "openapi-gateway/h5Pay", map);
        log.info("[{}] 糯米支付下单返回:{}", vo.getOut_trade_no(), s);

        if (s.startsWith("{")) {
            JSONObject parse = JSONObject.parseObject(s);
            JSONObject order_rsp = parse.getJSONObject("order_rsp");
            String return_code = order_rsp.getString("return_code");
            String return_msg = order_rsp.getString("return_msg");
            throw new ApiValidateException(return_code + "|" + return_msg);
        } else {

        }

        String setform = redisService.setPyformString(s);
        return setform;


    }


    public Map<String, String> dodfpay(PaymentVo vo, String srcmch_id) throws ApiValidateException {

        String channelString = redisService.getPaymentChannelInfo(vo.getMch_id(), srcmch_id);
        SysPaymentChannel sysPaymentChannel = new Gson().fromJson(channelString, SysPaymentChannel.class);

        Map<String, String> map = new TreeMap<>();
        map.put("merchant_id", sysPaymentChannel.getMerchantNo());
        map.put("reg_merchant_id", "");
        map.put("sign", "");
        map.put("order_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        map.put("order_no", vo.getOut_trade_no());
        map.put("notify_url", "http://www.baidu.com");
        map.put("subject", "资金来往");
        map.put("order_amount", NumberHelpler.cent2money(vo.getTrans_money()).toString());
        map.put("bank_account", vo.getBank_card());
        map.put("bank_name", vo.getBank_name());
        map.put("bank_branch", vo.getBankBranchname());
        map.put("bank_province", "北京市");
        map.put("bank_city", "北京市");
        map.put("user_name", vo.getAccount_name());
        map.put("card_type", "01");
        map.put("bank_telephone_no", "13111111111");
        map.put("id_card", "");
        map.put("bank_type", vo.getBankCode());
        MD5SignUtils.signMap(map, sysPaymentChannel.getSignKey());
        String s = HttpHelper.doPostForm(HOST_URL + "openapi-gateway/remit", map);
        log.info("[{}] 糯米支付代付返回:{}", vo.getOut_trade_no(), s);

        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONObject order_rsp = jsonObject.getJSONObject("order_rsp");

        String return_code = order_rsp.getString("return_code");

        if ("10000".equals(return_code)) {

        } else {
            throw new ApiValidateException(return_code + "|" + order_rsp.getString("return_msg"));
        }

        return null;
    }


    public Map doqueryDf(String orderNo, String mch_id, String srcMchId) throws Exception {
        String channelString = redisService.getPaymentChannelInfo(mch_id, srcMchId);
        SysPaymentChannel sysPaymentChannel = new Gson().fromJson(channelString, SysPaymentChannel.class);

        Map<String, String> map = new TreeMap<>();
        map.put("merchant_id", sysPaymentChannel.getMerchantNo());
        map.put("sign", "");
        map.put("order_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        map.put("order_no", orderNo);
        MD5SignUtils.signMap(map, sysPaymentChannel.getSignKey());
        String s = HttpHelper.doPostForm(HOST_URL + "/openapi-basis-gateway/queryDf", map);
        log.info("【{}】代付查询返回结果:{}", orderNo, s);

        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONObject order_rsp = jsonObject.getJSONObject("order_rsp");
        JSONObject data = order_rsp.getJSONObject("data");

        Map map1 = data.toJavaObject(Map.class);

        return map1;
    }
}
